package com.lambda.demo5.demo3;

import java.util.function.Consumer;

/**
 * @ClassName Test
 * @Description 定义一个学生类（姓名、年龄）；定义一个数组，存储若干学生；
 * 定义三个Consumer，分别用于打印输出姓李的学生信息、
 * 名字有三个字的学生信息、年龄大于18岁的学生
 * @Author Kx
 * @Date 2025/4/2 10:54
 * @Version 1.0
 */
public class Test {
    public static void main(String[] args) {
        // 定义学生对象数组
        Student[] students = {new Student("李明", 20), new Student("张大海", 18), new Student("李五", 19), new Student("王小六", 20)};

        // 检测姓李的学生
        Consumer consumer1  = (student1) -> {
            for (Student student : students) {
                // 判断姓名是否以“李”开头
                if (student.getName().startsWith("李")) {
                    System.out.println(student.getName() + "-" + student.getAge());
                }
            }
        };

        // 检测名字有三个字的学生
        Consumer consumer2  = (student2) -> {
            for (Student student : students) {
                if (student.getName().length() == 3) {
                    System.out.println(student.getName() + "-" + student.getAge());
                }
            }
        };

        // 检测年龄大于18岁的学生
        Consumer consumer3  = (student3) -> {
            for (Student student : students) {
                if (student.getAge() > 18) {
                    System.out.println(student.getName() + "-" + student.getAge());
                }
            }
        };
//        consumer1.accept(students);
//        System.out.println("------------------");
//        consumer2.accept(students);
//        System.out.println("------------------");
//        consumer3.accept(students);

        consumer1.andThen(consumer2).andThen(consumer3).accept(students);
    }
}
